<?php

/*
 * This class is used for as Model for Request table 
 */

class Model_Assignrequest extends Model_Base {

    protected $_name = 'tbl_request_assign';
    protected $_primaryKey = 'ass_id';

  
  

    /**
     * Function to return different status of the Assign Request
     *
     * @param  null
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function getStatusArray() {
        return array(
            '6' => 'Assign',
            '3' => 'Done',
            '4' => 'Pending',
            '5' => 'In Progress',
            '7' => 'Seen'
            
        );
    }

    /**
     * Fetch an individual entry for view
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryByViewId($id) {

        $select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('request' => 'tbl_request_assign'))
                ->join(array('lgn' => 'tbl_login'), "lgn.lgn_id = request.updated_lgn_id")
                ->where('ass_id = ?', $id)
                ->where('ass_status != 2');
        // see reasoning in fetchEntries() as to why we return only an array
        #echo $select;exit;
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('request' => 'tbl_request_assign'))
                ->setIntegrityCheck(false)
                ->where('ass_id = ?', $id)
                ->where('ass_status != 2');
        // see reasoning in fetchEntries() as to why we return only an array

        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    
    
    public function fetchEntryByrequestId($id) {
        $select = $this->select()
                ->from(array('request' => 'tbl_request_assign'))
                ->setIntegrityCheck(false)
                ->where('ass_req_id = ?', $id)
                ->where('ass_status != 2');
        // see reasoning in fetchEntries() as to why we return only an array

        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get requests
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchAssignrequest($where = "", $limit = "", $offset = "0", $order = 'ass_id', $sort = 'ASC') {
      // echo $where;exit;
        $select = $this->select()
                ->from(array('request' => 'tbl_request_assign'))
                ->setIntegrityCheck(false)
                ->where('ass_status != 2');
        if ($where != '') {
          //  echo 'isn';exit;
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }

        $select->order($order . " " . $sort);
      //  echo $select; exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get total orders
     * @return int
     */
    public function getAssignrequestCount($where) {
        $select = $this->select()
                ->from(array('request' => 'tbl_request_assign'), Array('cnt' => 'count(*)'))
                ->setIntegrityCheck(false)
                ->where('ass_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }

    /**
     *  This function is used to delete requests
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function deleteAssignrequest($prdtArray) {

        if (is_array($prdtArray)) {
            foreach ($prdtArray as $_request) {
                $requestData = Array();
                $requestData = $this->fetchEntryById($_request);
                if (count($requestData) > 0) {
                    $data = array('ass_status' => '2');
                    $this->update($data, 'ass_id =' . (int) $_request);
                }
            }
        } else {
            $ass_id = $prdtArray;
            $requestData = $this->fetchEntryById($ass_id);
            if (count($requestData) > 0) {
                $data = array('ass_status' => '2');
                $this->update($data, 'ass_id =' . $ass_id);
            }
        }
    }

}
